/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormTarefa.java
 *
 * Created on 21/05/2012, 21:03:32
 */
package views;

import java.awt.Component;
import javax.swing.DefaultComboBoxModel;
import models.Cronograma;
import models.Projeto;
import models.Tarefa;

/**
 *
 * @author 40800431
 */
public class FormTarefa extends javax.swing.JPanel {
    private final MainWindow parentFrame;

    /** Creates new form FormTarefa */
    public FormTarefa(MainWindow parentFrame) {
        initComponents();
        
        this.parentFrame = parentFrame;
        
        DefaultComboBoxModel projetosModel = new DefaultComboBoxModel(Projeto.listAllAsArray());
        projetosModel.insertElementAt("Selecione um Projeto", 0);
        this.cboProjeto.setModel(projetosModel);
        this.cboProjeto.setSelectedIndex(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNome = new javax.swing.JLabel();
        lblHoras = new javax.swing.JLabel();
        lblDescrição = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        txtHoras = new javax.swing.JTextField();
        paneDescricao = new javax.swing.JScrollPane();
        textAreaDescricao = new javax.swing.JTextArea();
        btnCadastrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblProjeto = new javax.swing.JLabel();
        lblCronograma = new javax.swing.JLabel();
        cboProjeto = new javax.swing.JComboBox();
        cboCronograma = new javax.swing.JComboBox();

        lblNome.setText("Nome");

        lblHoras.setText("Horas Previstas");

        lblDescrição.setText("Descrição");

        textAreaDescricao.setColumns(20);
        textAreaDescricao.setRows(5);
        paneDescricao.setViewportView(textAreaDescricao);

        btnCadastrar.setText("Cadastrar");
        btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCadastrarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");

        lblProjeto.setText("Projeto");

        lblCronograma.setText("Cronograma");

        cboProjeto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboProjetoActionPerformed(evt);
            }
        });

        cboCronograma.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(paneDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblProjeto)
                            .addComponent(lblNome))
                        .addGap(66, 66, 66)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cboProjeto, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtNome, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cboCronograma, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnCadastrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCronograma)
                            .addComponent(lblDescrição)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblHoras)
                                .addGap(18, 18, 18)
                                .addComponent(txtHoras, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblProjeto)
                    .addComponent(cboProjeto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCronograma)
                    .addComponent(cboCronograma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNome)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblHoras)
                    .addComponent(txtHoras))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDescrição)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(paneDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCadastrar)
                    .addComponent(btnCancelar)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCadastrarActionPerformed
        Component[] allComponents = this.getComponents();
        for(int i = 0; i < allComponents.length; i++){
            allComponents[i].setEnabled(false);
        }
        
        Projeto proj = new Projeto(this.cboProjeto.getSelectedItem().toString());
        Cronograma cron = new Cronograma();
        cron.setProjeto(proj);
        cron.load(this.cboCronograma.getSelectedItem().toString());
        
        
        Tarefa novaTarefa = new Tarefa();
        novaTarefa.setNome(txtNome.getText());
        novaTarefa.setDescricao(textAreaDescricao.getText());
        novaTarefa.setHorasPrevistas(Integer.parseInt(txtHoras.getText()));
        novaTarefa.setCronograma(cron);
        
        if(novaTarefa.insert()){
            parentFrame.returnToHome();
        }
        
        
    }//GEN-LAST:event_btnCadastrarActionPerformed

    private void cboProjetoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboProjetoActionPerformed
        if(this.cboProjeto.getSelectedIndex() > 0){
            String selectedProject = this.cboProjeto.getSelectedItem().toString();

            String[] cronogramas = Cronograma.listByProject(selectedProject);

            this.cboCronograma.setModel(new javax.swing.DefaultComboBoxModel(cronogramas));
            this.cboCronograma.setEnabled(true);
        }else{
            this.cboCronograma.setEnabled(false);
            this.cboCronograma.setModel(new javax.swing.DefaultComboBoxModel());
        }
    }//GEN-LAST:event_cboProjetoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cboCronograma;
    private javax.swing.JComboBox cboProjeto;
    private javax.swing.JLabel lblCronograma;
    private javax.swing.JLabel lblDescrição;
    private javax.swing.JLabel lblHoras;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblProjeto;
    private javax.swing.JScrollPane paneDescricao;
    private javax.swing.JTextArea textAreaDescricao;
    private javax.swing.JTextField txtHoras;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables
}
